package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.fragments.RegistFragment;
import com.sxm.yiwei.tools.REXTools;
import com.sxm.yiwei.utils.AESUtil;
import com.sxm.yiwei.utils.InternetAddressUtis;
import com.sxm.yiwei.views.ValidationCode;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class InstallPsgActivity extends BaseActivity implements View.OnClickListener{


    private ImageView psdback;
    private EditText onequestion;
    private EditText twoquestion;
    private EditText threequestion;
    private EditText inpsd;
    private EditText inconfrimpsd;
    private Button surebt;
    private EditText inphone;
    private ValidationCode piccode;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String type;
    private Button getphonecode;
    private String data;
    private String messageId;
    private String phone;
    private String inpiccode;
    private String inphonecode;
    private String city;
    private String fruit;
    private String school;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sou_suo_number);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");
        initBtView();

    }
        // 初始化控件
    private void initBtView() {
        backmain = (ImageView) findViewById(R.id.instrallpsg_backmain);
        inphone = (EditText) findViewById(R.id.instrall_phone);
        psdback = (ImageView) findViewById(R.id.instrall_psdback);
        onequestion = (EditText) findViewById(R.id.insrtall_inquestionone);
        twoquestion = (EditText) findViewById(R.id.insrtall_inquestiontwo);
        threequestion = (EditText) findViewById(R.id.insrtall_inquestionthree);
        inpsd = (EditText) findViewById(R.id.instrall_inpiccode);
        inconfrimpsd = (EditText) findViewById(R.id.insrtall_inphonecode);
        surebt = (Button) findViewById(R.id.insrtall_surebt);
        piccode = (ValidationCode) findViewById(R.id.instrall_piccode);
        getphonecode = (Button) findViewById(R.id.instrall_getcode);
        psdback.setOnClickListener(this);
        surebt.setOnClickListener(this);
        getphonecode.setOnClickListener(this);
        backmain.setOnClickListener(this);

    }

    // 获取短信验证码
    public void getCode() {
        getInData();
        RequestParams param1 = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.GETCODE);// 验证码接口
        param1.addHeader("Content-Type","application/json");
        param1.addHeader("appId", appId);
        param1.addHeader("userId", userId);
        param1.addHeader("token", token);
        com.alibaba.fastjson.JSONObject object = new com.alibaba.fastjson.JSONObject();

        object.put("mobile",phone);
        object.put("type","4");
        param1.addParameter("params",object.toJSONString());
        x.http().get(param1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                // 获取appid
                data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                messageId = obj1.getString("messageId");

            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {

            }
        });
    }

        // 输入内容
    private void getInData() {
        phone = String.valueOf(inphone.getText());
        inpiccode = String.valueOf(inpsd.getText());
        inphonecode = String.valueOf(inconfrimpsd.getText());

        city = String.valueOf(onequestion.getText());
        fruit = String.valueOf(twoquestion.getText());
        school = String.valueOf(threequestion.getText());

    }

    @Override
    public void onClick(View v) {
        getInData();
        switch (v.getId()){
            case R.id.instrallpsg_backmain:
                startActivity(new Intent(InstallPsgActivity.this,MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.instrall_psdback:
                finish();
                break;
            case R.id.instrall_getcode:
                if (TextUtils.isEmpty(phone)){
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(city)){
                    Toast.makeText(this, "请输入密保问题", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(fruit)){
                    Toast.makeText(this, "请输入密保问题", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(school)){
                    Toast.makeText(this, "请输入密保问题", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(inpiccode)){
                    Toast.makeText(this, "请输入图形验证码", Toast.LENGTH_SHORT).show();
                }else {

                    if (REXTools.isPhoneNumber(phone)){
                        if (inpiccode.equals(piccode.getCodeString())){
                            MyTimer timer = new MyTimer(120000,1000);
                            timer.start();
                            getCode();
                        }else {
                            Toast.makeText(this, "图形验证码错误", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                    }


                }


                break;
            case R.id.insrtall_surebt:

                if (TextUtils.isEmpty(phone)){
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(city)){
                    Toast.makeText(this, "请输入密保问题", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(fruit)){
                    Toast.makeText(this, "请输入密保问题", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(school)){
                    Toast.makeText(this, "请输入密保问题", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(inpiccode)){
                    Toast.makeText(this, "请输入图形验证码", Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(inphonecode)){
                    Toast.makeText(this, "请输入手机验证码", Toast.LENGTH_SHORT).show();
                }else {

                    Intent intent = new Intent(InstallPsgActivity.this,InstrallPayPsdTwoActivity.class);
                    intent.putExtra("messageId",messageId);
                    intent.putExtra("city",city);
                    intent.putExtra("fruit",fruit);
                    intent.putExtra("school",school);
                    intent.putExtra("code",inphonecode);
                    intent.putExtra("from","install");
                    intent.putExtra("url",InternetAddressUtis.SETYIVIPAYWORD);
                    startActivity(intent);
                    finish();
                }
                break;
        }
    }
    //120s 倒计时
    class MyTimer extends CountDownTimer {
        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                  倒计时数        to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *           倒计时间隔               {@link #onTick(long)} callbacks.
         */
        public MyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            getphonecode.setClickable(false);
            getphonecode.setText(millisUntilFinished/1000+"s");
        }

        @Override
        public void onFinish() {
            getphonecode.setClickable(true);
            getphonecode.setText("验证码");
        }
    }
}
